var select = require('{plus}/select');
var request = require('request');

var imitSelect = $.fn.imitSelect;

$.fn.imitSelect = function(opts){
    if(!opts){
        opts = {};
    }
    opts.icon = '&#xe636;';
    return imitSelect.call(this, opts)
}

return function(selector, url, data, text, options, fields){
    if(typeof data !== 'object'){
        fields = options;
        options = text;
        text = data;
        data = {};
    }

    if(!fields){
        fields = {
            list:'list',
            text:'text',
            value:'value'
        }
    }

    var option = '<option value="">'+ (text || '请选择') +'</option>';
    request.get(url, data, function(res){
        if(res){
            var list = res.data[fields.list];
            if(list && list.length){
                Nui.each(list, function(v, k){
                    option += '<option value="'+ v[fields.value] +'">'+ v[fields.text] +'</option>'
                })
            }
        }
        selector.html(option).imitSelect(options)
    }, null)
}